import cplex
import random
import numpy as np
#from instances import fscplex
from ccfs.fscplex import fscplex
from ccfs.callbacks import CheckBigM
from ccfs.callbacks import Reject
# Generate instances and read in parameters
fc = fscplex()
fc.generate_instance(NI = 15, NS=15, eps=0.2, Ordered=False)
fc.parameters.read_file("../param/stofac.prm")

fc.strengthen_formulation()

fc.write("fc_stre.lp")
fc.solve()
x_val = fc.solution.get_values(fc.x_name)
y_val = fc.solution.get_values(fc.y_name)
obj_val = fc.solution.get_objective_value()

delta = 0.1
bigm_val = [0] * fc.num_bigm
for s in fc.S:
#    if y_val[s] == 1.0:
        for i in fc.I:
            ind = i + s*fc.NI
            bigm_val[ind] = 0.2
            #bigm_val[ind] = max(0, fc.rhs[s][i] - x_val[i]-0.5)

fc.set_bigm(bigm_val)
fc.register_callback(CheckBigM)
#fc.register_callback(Reject)

iter = 0
# Start check and resolve procedure
while(iter < 200):
    print "----------------------"
    print "Iteration ", iter
    print "----------------------"
    iter += 1

    # resolve the problem
    fc.flag[0] = 1
    fc.solve()

    if fc.flag[0] == 1: #pass all the test
        print "finish loop"
        print fc.solution.progress.get_num_nodes_processed()
        break
    
    elif fc.flag[0] == 0: #binding being checked
        print fc.solution.progress.get_num_nodes_processed()
        #fc.increase_bigm(step_length=delta)
        fc.increase_bigm(step_length=delta)
        # increase the bigm here

fc.unregister_callback(CheckBigM)

#fc.solve()
fc.write("fc_bigm.lp")

print "Ture obj", obj_val
print "My obj", fc.solution.get_objective_value() 
